import { Component, OnInit, ViewChild, ViewContainerRef, ComponentRef, ComponentFactoryResolver, EventEmitter } from '@angular/core';
import { DataService } from '../../../../../services/data.service';
import { ModifyService } from './modify.service';
import { ApplyFundGroupComponent } from '../apply-fund-group/apply-fund-group.component';
import { NapplyFundGroupComponent } from '../napply-fund-group/napply-fund-group.component'
import * as _ from 'lodash';
import { Util } from 'app/common/util';

@Component({
  selector: 'app-modify',
  templateUrl: './modify.component.html',
  styleUrls: ['./modify.component.scss'],
  providers: [ModifyService]
})
export class ModifyComponent implements OnInit {
  //查看#applyFundGroup modal7
  @ViewChild("applyFundGroup", { read: ViewContainerRef })
  applyFundGroup: ViewContainerRef;
  applyFundGroupComponent: ComponentRef<ApplyFundGroupComponent>;
  //查看#napplyFundGroup modal8
  @ViewChild("napplyFundGroup", { read: ViewContainerRef })
  napplyFundGroup: ViewContainerRef;
  napplyFundGroupComponent: ComponentRef<NapplyFundGroupComponent>;

  //modal创建时传值
  public item: any;
  //克隆itemClone
  public itemClone: any;
  //克隆重置数据itemCloneReset
  public itemCloneReset: any;

  // 用于判断选中项是否改变
  public differenceItem: any = []

  constructor(
    public resolver: ComponentFactoryResolver,
    public modifyService: ModifyService,
    public dataService: DataService
  ) { }

  ngOnInit() {
    console.log(this.item);
    //加载时克隆item
    this.itemClone = _.cloneDeep(this.item);
    this.itemCloneReset = _.cloneDeep(this.item);
    this.dataService.nQDIIsFgMemberValidEmitter = new EventEmitter();
    this.dataService.nQDIIsFgMemberValidEmitter.subscribe((value: any) => {
      let valueArray = [];
      let oldData = []
      // if (value) {
      //   this.itemClone.sMemberTemp = value;
      //   window['$']('#applyFundGroup').modal('hide');
      //   window['$']('#napplyFundGroup').modal('hide');
      // } else {
      //   window['$']('#applyFundGroup').modal('hide');
      //   window['$']('#napplyFundGroup').modal('hide');
      //   window['swal']("提示", "没有选择数据!", "warning");
      // }
      console.log(value);
      console.log(typeof value);
      if (value == 'no data') {
        console.log('清空数据')
        window['swal']("提示", "没有选择数据!", "warning");
        this.itemClone.sMemberTemp = '';
        window['$']('#applyFundGroup').modal('hide');
        window['$']('#napplyFundGroup').modal('hide');

      }
      else if (value !== 'no data' && value != '') {
        if (this.itemClone.sMemberTemp == null) {
          this.itemClone.sMemberTemp = value;
          console.log(this.itemClone.sMemberTemp);
          window['$']('#applyFundGroup').modal('hide');
          window['$']('#napplyFundGroup').modal('hide');
        }
        else {
          console.log('直接赋值')
          // 如果非空，转成数组,用于判断值是否修改。
          valueArray = value.split(',')
          console.log(this.itemClone.sMemberTemp)
          console.log(typeof this.itemClone.sMemberTemp);
          oldData = this.itemClone.sMemberTemp.split(',');
          let differenceItem = oldData
            .filter(x => valueArray.indexOf(x) == -1)
            .concat(valueArray.filter(x => oldData.indexOf(x) == -1));

          // let differenceItem = _.difference(valueArray, oldData);
          console.log(_.difference(valueArray, oldData));
          console.log(valueArray)
          console.log(oldData)
          console.log(differenceItem);
          if (differenceItem.length == 0) {
            window['$']('#applyFundGroup').modal('hide');
            window['$']('#napplyFundGroup').modal('hide');
            window['swal']("提示", "没有选择数据!", "warning");
          } else {
            // console.log(typeof this.addBody.sMemberTemp)
            this.itemClone.sMemberTemp = value;
            console.log(this.itemClone.sMemberTemp);
            window['$']('#applyFundGroup').modal('hide');
            window['$']('#napplyFundGroup').modal('hide');
          }
        }

      }
    });
    this.dataService.nQDIIsMemberFundnosEmitter.subscribe((value?: any) => {

      if (value == 'no data') {
        this.itemClone.sMemberFundnos = '';
      }

      else if (value !== 'no data' && value != '') {
        let valueArray = value.split(',')
        let oldData = this.itemClone.sMemberTemp.split(',');
        let differenceItem = _.difference(valueArray, oldData);
        if (differenceItem.length == 0) {
          this.itemClone.sMemberFundnos = value;
         }
        else {
          this.itemClone.sMemberFundnos = value;
        }
      }
      // if (value) {
      //   console.log(value);
      //   this.itemClone.sMemberFundnos = value;
      //   console.log(this.itemClone.sMemberFundnos);
      // }


    });
  }

  //重置数据
  resetModifyData() {
    this.item.sFgName = this.itemCloneReset.sFgName;
    this.item.sFgDesc = this.itemCloneReset.sFgDesc;
    this.itemClone.sMemberTemp = this.itemCloneReset.sMemberTemp;
    this.itemClone.sMemberFundnos = this.itemCloneReset.sMemberFundnos;
  }

  // 保存修改内容
  changeModifyData() {
    let sOprAction: any;
    if (this.item.sStatus == "修改待审") {
      sOprAction = "update";
    } else if (this.item.sStatus == "添加待审") {
      sOprAction = "insert";
    };
    console.log(sOprAction);
    let putBody: any = {
      nFgId: this.item.nFgId,
      sType: this.item.sType,
      // sOprAction:this.item.sOprAction,
      sFgName: this.item.sFgName,
      sFgDesc: this.item.sFgDesc,
      sFgMemberValid: this.item.sFgMemberValid,
      sMemberTemp: this.itemClone.sMemberTemp,
      sMemberFundnos: this.itemClone.sMemberFundnos,
      // sStatus: this.item.sStatus,
    }
    console.log(putBody);
    if (this.item.sFgName != "") {
      //对象是否相等校验
      if (Util.objectCheck(this.item, this.itemClone)) {
        window['swal']("提示", "信息未修改，不会生成修改审核记录", "warning");
        this.dataService.rcmiEditEmitter.emit('modify');
      } else {
        console.log(this.item.sStatus)
        if (this.item.sStatus == "正常") {       // || this.item.sStatus == "修改待审"
          this.modifyService.modify(this.item.sType, putBody).subscribe(data => {
            if (data) {
              console.log(data);
              this.dataService.rcmiEditEmitter.emit('modify');
              window['swal']("成功", "请等待修改审核", "success");
            }
          });
        } else {
          this.modifyService.addModify(this.item.sType, sOprAction, putBody).subscribe(data => {
            if (data) {
              console.log(data);
              this.dataService.rcmiEditEmitter.emit('modify');
              window['swal']("成功", "修改成功", "success");
            }
          });
        }
      }
    } else {
      window['swal']("提示", "请填写:群组名称", "warning");
    }
  }

  //显示#showApplyFundGroup modal7
  showApplyFundGroupModal() {
    if (this.item.sType == "1") {
      this.showApplyFundGroup();
      window['$']('#applyFundGroup').modal('show');
    } else if (this.item.sType == "0") {
      this.showNapplyFundGroup();
      window['$']('#napplyFundGroup').modal('show');
    }
  }

  //查看#showApplyFundGroup modal7创建
  showApplyFundGroup() {
    // 判断组件是否已创建，如果创建先注销
    if (this.applyFundGroupComponent && this.applyFundGroupComponent.instance) {
      this.applyFundGroupComponent.destroy();
    }
    // 动态创建组件的实例
    const applyFundGroupComponent = this.resolver.resolveComponentFactory(ApplyFundGroupComponent);
    this.applyFundGroupComponent = this.applyFundGroup.createComponent(applyFundGroupComponent);
    if (this.itemClone.sMemberFundnos != '' && this.itemClone.sMemberFundnos != null && this.itemClone.sMemberFundnos != undefined) {
      this.applyFundGroupComponent.instance.checkedIdList = this.itemClone.sMemberFundnos;
    }

  }

  //查看#showNapplyFundGroup modal8创建
  showNapplyFundGroup() {
    // 判断组件是否已创建，如果创建先注销
    if (this.napplyFundGroupComponent && this.napplyFundGroupComponent.instance) {
      this.napplyFundGroupComponent.destroy();
    }
    // 动态创建组件的实例
    const napplyFundGroupComponent = this.resolver.resolveComponentFactory(NapplyFundGroupComponent);
    this.napplyFundGroupComponent = this.napplyFundGroup.createComponent(napplyFundGroupComponent);
    if (this.itemClone.sMemberFundnos != '' && this.itemClone.sMemberFundnos != null && this.itemClone.sMemberFundnos != undefined) {
      this.napplyFundGroupComponent.instance.checkedIdList = this.itemClone.sMemberFundnos;
    }
  }

  // 关闭modal时解锁
  closeModal() {
    let getbody = {
      nFgIdList: []
    }
    getbody.nFgIdList.push(this.item.nFgId)
    console.log(getbody);
    this.modifyService.unlock(getbody).subscribe((data) => {
    })
  }
}
